﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace BinaryStudent
{
    class Program
    {
        private const string FILE = @"input.bin";
        static void Main(string[] args)
        {
            Student student = new Student("Milen","Stanoev",71038);
            //AddToFile(student, FILE);
            //Student student1 = new Student("Milennn", "Stanoev", 771038);
           // AddToFile(student1, FILE);
            PrintFile(FILE);
            DeleteFromFile(student,FILE);
            Console.WriteLine();
            PrintFile(FILE);
        }
        public static void AddToFile(Student student, string file)
        {
            FileStream filestream = new FileStream(file, FileMode.Open);
            string tempFile = "output.bin";
            FileStream filestreamtemp = new FileStream(tempFile, FileMode.Create);
            using (BinaryReader inFile = new BinaryReader(filestream))
            {
                using (BinaryWriter outFile = new BinaryWriter(filestreamtemp))
                {
                    while (inFile.BaseStream.Position < inFile.BaseStream.Length)
                    {
                        string firstName = inFile.ReadString();
                        outFile.Write(firstName);
                        string lastName = inFile.ReadString();
                        outFile.Write(lastName);
                        int facNum = inFile.ReadInt32();
                        outFile.Write(facNum);

                    }
                    outFile.Write(student.FirstName);
                    outFile.Write(student.LastName);
                    outFile.Write(student.FacultyNumber);
                }
            }
            File.Delete(file);
            File.Move(tempFile, file);
        }

        public static void PrintFile(string file)
        {
            FileStream filestream = new FileStream(file, FileMode.Open);

            using (BinaryReader inFile = new BinaryReader(filestream))
            {

                    while (inFile.BaseStream.Position < inFile.BaseStream.Length)
                    {
                        string firstName = inFile.ReadString();
                        Console.WriteLine(firstName);
                        string lastName = inFile.ReadString();
                        Console.WriteLine(lastName);
                        int facNum = inFile.ReadInt32();
                        Console.WriteLine(facNum);

                    }
            }

        }

        public static void DeleteFromFile(Student student, string file)
        {
            FileStream filestream = new FileStream(file, FileMode.Open);
            string tempFile = "output.bin";
            FileStream filestreamtemp = new FileStream(tempFile, FileMode.Create);
            using (BinaryReader inFile = new BinaryReader(filestream))
            {
                using (BinaryWriter outFile = new BinaryWriter(filestreamtemp))
                {
                    while (inFile.BaseStream.Position < inFile.BaseStream.Length)
                    {
                        string firstName = inFile.ReadString();                      
                        string lastName = inFile.ReadString();
                        int facNum = inFile.ReadInt32();
                       
                        if (!(firstName == student.FirstName && lastName == student.LastName && facNum == student.FacultyNumber))
                        {
                            outFile.Write(firstName);
                            outFile.Write(lastName);
                            outFile.Write(facNum);
                        }

                    }

                }
            }
            File.Delete(file);
            File.Move(tempFile, file);
        }
    }
}
